package cn.bnuz.edu.todolist;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;

import cn.bnuz.edu.todolist.database.TodoDB;
import cn.bnuz.edu.todolist.pojo.Event;

public class AddTodoList extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_event_page);

        Button button_back = (Button) findViewById(R.id.back);
        Button button_save = (Button) findViewById(R.id.save);


        final EditText title = (EditText) findViewById(R.id.edit_title);
        final TextView date = (TextView) findViewById(R.id.edit_date);


        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddTodoList.this,MainActivity.class);
                startActivity(intent);
            }
        });

        button_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String event_title = title.getText().toString(),
                        event_date = date.getText().toString();
                Event event = new Event();
                event.setEventTitle(event_title);
                event.setEventTime(event_date);
                if (TodoDB.addEvent(event)){
                    Intent intent = new Intent(AddTodoList.this,MainActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(AddTodoList.this,R.string.db_null_error,Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddTodoList.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });


        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar ca = Calendar.getInstance();
                final int[] mYear = {ca.get(Calendar.YEAR)};
                final int[] mMonth = {ca.get(Calendar.MONTH)};
                final int[] mDay = {ca.get(Calendar.DAY_OF_MONTH)};

                DatePickerDialog datePickerDialog = new DatePickerDialog(AddTodoList.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                mYear[0] = year;
                                mMonth[0] = month;
                                mDay[0] = dayOfMonth;
                                final String data =  year+"年"+(month+1) + "月" + dayOfMonth + "日 ";
                                date.setText(data);
                            }
                        },
                        mYear[0], mMonth[0], mDay[0]);
                datePickerDialog.show();

                System.out.println(datePickerDialog.getDatePicker().getYear());
                System.out.println(datePickerDialog.getDatePicker().getMonth());
                System.out.println(datePickerDialog.getDatePicker().getDayOfMonth());
                System.out.println("day:"+mDay[0]+"mon:"+mMonth[0]+"year:"+mYear[0]);
            }
        });





    }
}
